package com.enjoy.activemq.api;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Auther guwenbo
 * @Date 2018/11/23
 */
public class Producer {

    private static final String USER_NAME =
//            "admin";
        ActiveMQConnection.DEFAULT_USER;

    private static final String PASSWORD =
//            "admin";
        ActiveMQConnection.DEFAULT_PASSWORD;

    private static final String BROKE_URL =
            "tcp://47.99.197.150:61616";
//        ActiveMQConnection.DEFAULT_BROKER_URL;


    public static void startProducer(){
        ConnectionFactory factory;
        Connection connection;
        Session session;
        Destination destination;
        MessageProducer messageProducer;


        factory = new ActiveMQConnectionFactory(USER_NAME,PASSWORD,BROKE_URL);

        try {
            connection = factory.createConnection();
            connection.start();
            session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
//            destination = session.createTopic("guwenboMQ");
            destination = session.createQueue("guwenboMQ");
            messageProducer = session.createProducer(destination);

            for (int i = 0; i < 3; i++) {
                String msg = "发送消息"+i+" "+System.currentTimeMillis();
                System.out.println(msg);
                Message message = session.createTextMessage(msg);
                messageProducer.send(message);
            }



        } catch (JMSException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        startProducer();
    }

}
